package com.app.signing.view.room.view;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.app.signing.R;
import com.app.signing.view.room.data.RoomSeatData;
import com.app.signing.view.room.data.SeatType;

import java.util.List;

/**
 * @author wangpeifeng
 * @date 2018/05/09 9:37
 */
public class TableView extends FrameLayout implements View.OnClickListener {

    private static int[] viewResId = {
            R.id.text_seat_0, R.id.text_seat_1, R.id.text_seat_2, R.id.text_seat_3,
            R.id.text_seat_4, R.id.text_seat_5, R.id.text_seat_6, R.id.text_seat_7,
            R.id.text_seat_8, R.id.text_seat_9, R.id.text_seat_10, R.id.text_seat_11
    };
    private TextView[] textViews = new TextView[12];
    private List<RoomSeatData> roomSeatDataList;

    private OnSeatClickListener onSeatClickListener;

    public TableView(@NonNull Context context) {
        this(context, null);
    }

    public TableView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TableView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        View view = LayoutInflater.from(context).inflate(R.layout.layout_grid_table, this, true);
        for (int i = 0; i < textViews.length; i++) {
            TextView textView = view.findViewById(viewResId[i]);
            textView.setOnClickListener(this);
            textViews[i] = textView;
        }
    }

    public void setSeatDat(List<RoomSeatData> roomSeatDataList) {
        if (roomSeatDataList == null) {
            return;
        }
        this.roomSeatDataList = roomSeatDataList;
        for (int i = 0; i < textViews.length; i++) {
            TextView textView = textViews[i];
            textView.setBackground(null);
            textView.setText("");
            RoomSeatData seatData = roomSeatDataList.get(i);
            if (seatData == null) {
                continue;
            }
            int type = seatData.getType();
            if (type == SeatType.seatTable) {
                textView.setBackground(
                        ContextCompat.getDrawable(getContext(), R.drawable.ic_table));
            } else if (type == SeatType.seatNo) {
                textView.setText(seatData.getId());
                textView.setBackground(
                        ContextCompat.getDrawable(getContext(), R.drawable.seat_white));
                textView.setTextColor(ContextCompat.getColor(getContext(), R.color.primary_text));
            } else if (type == SeatType.seatRed) {
                textView.setText(seatData.getId());
                textView.setBackground(
                        ContextCompat.getDrawable(getContext(), R.drawable.seat_red));
                textView.setTextColor(ContextCompat.getColor(getContext(), R.color.icons));
            } else if (type == SeatType.seatYellow) {
                textView.setText(seatData.getId());
                textView.setBackground(
                        ContextCompat.getDrawable(getContext(), R.drawable.seat_yellow));
                textView.setTextColor(ContextCompat.getColor(getContext(), R.color.icons));
            }
        }
    }

    public void setOnSeatClickListener(OnSeatClickListener onSeatClickListener) {
        this.onSeatClickListener = onSeatClickListener;
    }

    @Override
    public void onClick(View v) {
        int index = findView(v);
        if (index == -1) {
            return;
        }
        if (roomSeatDataList == null || roomSeatDataList.isEmpty()) {
            return;
        }
        RoomSeatData roomSeatData = roomSeatDataList.get(index);
        if (roomSeatData == null || roomSeatData.getType() == SeatType.seatTable) {
            return;
        }
        if (onSeatClickListener != null) {
            onSeatClickListener.onClick(roomSeatData);
        }
    }

    private int findView(View view) {
        int id = view.getId();
        for (int i = 0; i < textViews.length; i++) {
            TextView textView = textViews[i];
            if (id == textView.getId()) {
                return i;
            }
        }
        return -1;
    }
}
